﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace ProjectSem40701.Admin
{
    public partial class SponsorAdd : System.Web.UI.Page
    {
        //Guid sid;
        private const string SUCCESS = "success";
        private const string FAIL = "fail";

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                string strImageSource;

                if (this.fileUpSponsorImage.HasFile)
                {
                    string str = DateTime.Now.ToString("ddMMyyyyhhmmssms");
                    this.fileUpSponsorImage.SaveAs(Server.MapPath("~/images/" + str +
                        this.fileUpSponsorImage.FileName.Trim()));
                    strImageSource = str + this.fileUpSponsorImage.FileName.Trim();
                }
                else
                {
                    strImageSource = "noimage.jpg";
                }

                AdminXSD.SponsorTableAdapters.tbl_SponsorTableAdapter dp
                    = new AdminXSD.SponsorTableAdapters.tbl_SponsorTableAdapter();
                Guid uid = new Guid(this.cbbUser.SelectedValue.ToString());
                int ok = dp.tbl_Sponsor_Add(uid, this.txtSponsorName.Text.Trim(), this.txtAddress.Text.Trim(),
                    this.txtTel.Text.Trim(), this.txtEmail.Text.Trim(), Server.HtmlDecode(this.txtDescription.Value.Trim()),
                    int.Parse(this.cbbStatus.SelectedValue.ToString()), strImageSource);
                if (ok > 0)
                {
                    //bao thanh cong
                    Show_Dialog(SUCCESS);
                }
                else
                {
                    //bao that bai
                    Show_Dialog(FAIL);
                }


            }
            catch (Exception exc)
            {
                Show_Dialog(FAIL);
            }
        }

        private void Close_Dialog()
        {
            switch (this.lblSuccess.Text)
            {
                case SUCCESS:
                    Response.Redirect("Sponsor.aspx");
                    break;
                case FAIL:
                    this.btnAdd_ModalPopupExtender.Enabled = false;
                    Response.Redirect("Sponsor.aspx");
                    break;
                default:
                    this.btnAdd_ModalPopupExtender.Enabled = false;
                    break;
            }
        }
        private void Show_Dialog(String Check)
        {
            switch (Check)
            {
                case SUCCESS:
                    this.ltrMessage.Text = "Thành công";
                    break;
                case FAIL:
                    this.ltrMessage.Text = "Thất bại";
                    break;
                default:
                    this.ltrMessage.Text = "Không thành công";
                    break;
            }
            this.lblSuccess.Text = Check;
            this.btnAdd_ModalPopupExtender.Enabled = true;
            this.btnAdd_ModalPopupExtender.Show();
        }
        protected void btnClose_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }

        protected void btnYes_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }
    }
}
